package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallCategoryMapper;
import org.linlinjava.litemall.db.domain.LitemallCategory;
import org.linlinjava.litemall.db.domain.LitemallCategoryExample;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author jie
 * @date 2019/7/22 -10:49
 */
@Service
public class LitemallCategoryService {

    @Resource
    LitemallCategoryMapper categoryMapper;

    public LitemallCategory findById(Integer cid){
        return categoryMapper.selectByPrimaryKey(cid);
    }

    public List<LitemallCategory> getAllCategory(Integer page,Integer limit) {

        if (!StringUtils.isEmpty(page)&&!StringUtils.isEmpty(limit)){
            PageHelper.startPage(page, limit);
        }
        return categoryMapper.getAllCategory();
    }

    public void add(LitemallCategory category) {
        category.setAddTime(new Date());
        category.setUpdateTime(new Date());
        categoryMapper.insertSelective(category);
    }

    public List<LitemallCategory> queryL1() {
        LitemallCategoryExample example = new LitemallCategoryExample();
        example.or().andLevelEqualTo("L1").andDeletedEqualTo(false);
        return categoryMapper.selectByExample(example);
    }

    public void deleteById(Integer id) {
        categoryMapper.logicalDeleteByPrimaryKey(id);
    }

    public int updateById(LitemallCategory category) {
        category.setUpdateTime(new Date());
        return categoryMapper.updateByPrimaryKeySelective(category);
    }

    public List<LitemallCategory> queryByPid(Integer pid) {
        LitemallCategoryExample example = new LitemallCategoryExample();
        example.or().andPidEqualTo(pid).andDeletedEqualTo(false);
        return categoryMapper.selectByExample(example);
    }

    public long getL1CategoryCount(){
        LitemallCategoryExample example = new LitemallCategoryExample();
        example.or().andPidEqualTo(0).andDeletedEqualTo(false);
        return  categoryMapper.countByExample(example);
    }

    public List<LitemallCategory> queryChannel() {
        LitemallCategoryExample example = new LitemallCategoryExample();
        example.or().andLevelEqualTo("L1").andDeletedEqualTo(false);
        return categoryMapper.selectByExample(example);
    }

    public List<LitemallCategory> queryL1WithoutRecommend(int offset, int limit) {
        LitemallCategoryExample example = new LitemallCategoryExample();
        example.or().andLevelEqualTo("L1").andNameNotEqualTo("推荐").andDeletedEqualTo(false);
        PageHelper.startPage(offset, limit);
        return categoryMapper.selectByExample(example);
    }

    /**
     * 轻商城
     * @param ids
     * @return
     */
    public List<LitemallCategory> queryL2ByIds(List<Integer> ids) {
        LitemallCategoryExample example = new LitemallCategoryExample();
        example.or().andIdIn(ids).andLevelEqualTo("L2").andDeletedEqualTo(false);
        return categoryMapper.selectByExample(example);
    }

}
